package db_main

import (
	"fmt"
	"longmen/server/pkg/common/api_models"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_main"
	"strings"
)

func GetProtocolList() ([]*models_main.Protocol, error) {
	fName := "GetProtocolList"
	o := make([]*models_main.Protocol, 0)
	err := mysql.GetRTable(&models_main.Protocol{}).Select("id, title,content, url").Where("id<10").Find(&o).Error // 11 12 im 软件用
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}
func GetProtocol(r *api_models.GetProtocolByIdReq) (*models_main.Protocol, error) {
	fName := "GetProtocol"
	o := &models_main.Protocol{}
	table := mysql.GetRTable(&models_main.Protocol{}).Select("id, title, content, url").Where("id=?", r.ID)
	err := table.Find(&o).Error
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}

	if r.Channel != "" && r.ID > 1000 {
		var c models_main.RegSource
		mysql.GetRTable(&models_main.RegSource{}).Where("source_key=?", r.Channel).First(&c)
		o.Content = strings.Replace(o.Content, "{replace}", c.Description, -1)
		o.Content = strings.Replace(o.Content, "{about}", c.About, -1)
	}

	return o, err
}
